<?php
namespace app\wechat\controller;

use app\common\enums\ResultEnum;
use app\common\model\Article;
use app\common\model\Video;
use app\common\utils\ResultVOUtil;
use think\facade\Log;
use app\common\model\Category;

class Index
{
    public function index()
    {
        $article = new Article();
        $video = new Video();
        try {
            $data['article'] = $article->limit(10)->select();
            $data['video'] = $video->limit(10)->select();
            $data['banners'] = [
                ['url'=>'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg'],
                ['url'=>'https://cos.zi-m.cn//uploads/art/0029d6ade83341ee778f76c7867b800e.jpg'],
                ['url'=>'../images/20180710185712.png']
            ];
            
            return ResultVOUtil::success($data);
        } catch (\Exception $e) {
            Log::record('[首页列表]:'.$e->getMessage(),'error');
            return ResultVOUtil::error(ResultEnum::FIND_ERROR);
        }
    }
    
    public function discovery($search='',$length=5,$page=1,$cate=0) {
        
        $article = new Article();
        $video = new Video();
        $where = [];
        
        if ( !empty($search) ) {
            
            $where[] = ['title','like','%'.$search.'%'];
        }
        
        $cateModel = new Category();
        
        if($cate){
            try {
                $cid = $cateModel->where(['pid'=>$cate])->column('id');
            } catch (\Exception $e) {
            }
            $where[] = ['cid','in',$cid];
        }
        try {
            $data['article'] = $article->where($where)->paginate($length,false,[
                'type'     => 'bootstrap',
                'var_page' => 'page',
                'page'=>$page,
            ]);
            $data['video'] = $video->where($where)->paginate($length,false,[
                'type'     => 'bootstrap',
                'var_page' => 'page',
                'page'=>$page,
            ]);
        } catch (\Exception $e) {
            Log::record('[发现列表]:'.$e->getMessage(),'error');
            return ResultVOUtil::error(ResultEnum::FIND_ERROR);
        }
        return $data;
        
    }
}
